﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

/// <summary>
/// A lot stricter validator than the simple one. It will remove bad data e.t.c.
/// Currently same strictness tho except it also looks for duplicates
/// </summary>
public class Strict_ModelItemValidator : iModelItemValidator
{
    //The item to validate
    //private ModelItem m_item;
    private string m_errorMsg;

    public string ErrorMsg
    {
        get
        {
            return m_errorMsg;
        }
        set { m_errorMsg = value; }
    }

    public bool Validate(ModelItem a_item)
    {
        //m_item = a_item;       

        //Check name
        if (a_item.m_name.Length == 0)
        {
            m_errorMsg = "Model needs a name";
            return false;
        }
        //The model path
        else if (!Validator.ValidatePath(a_item.m_path, Strings.ModelItem.ModelPaths))
        {
            m_errorMsg = "Path is not valid";
            return false;
        }
        //Thumbnail path validation
        else if (!Validator.ValidatePath(a_item.m_thumbnailPath, Strings.ModelItem.ThumbnailPaths))
        {
            ErrorMsg = "Thumbnail is not valid";
            return false;
        }
        //Shader types
        else if (!ValidateShaders(a_item) )
        {
            m_errorMsg = "Shaders are not valid";
            return false;
        }
        //Textures!
        else if (!ValidateTextures(a_item) )
        {
            m_errorMsg = "Textures are not valid";
            return false;
        }

        return true;
    }

    public bool ValidateShaders(ModelItem a_item)
    {
        for (int i = 0; i < a_item.m_shaders.Count; )
        {
            if (!ValidateShader(a_item.m_shaders[i]))
            {
                //RemoveLine also removes the shader
                a_item.RemoveLine(a_item.m_shadersIDs[i]);
            }
            else
            {
                i++;
            }
        }

        return a_item.m_shaders.Count > 0;
    }

    private bool ValidateShader(string a_shaderName)
    {
        return String.Equals(a_shaderName, Strings.ModelItem.ShaderTypes.phong, StringComparison.OrdinalIgnoreCase) || String.Equals(a_shaderName, Strings.ModelItem.ShaderTypes.wireframe, StringComparison.OrdinalIgnoreCase);
    }

    public bool ValidateTextures(ModelItem a_item)
    {
        for (int i = 0; i < a_item.m_textures.Count; )
        {
            if (!Validator.ValidatePath(a_item.m_textures[i].m_path, Strings.ModelItem.TexturePaths))
            {
                //RemoveLine also removes the Texture
                a_item.RemoveLine(a_item.m_texturesIDs[i]);
            }
            else
            {
                i++;
            }
        }

        return a_item.m_textures.Count >= 0;
    }    
}